package com.dyzx.hbb.modules.car.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.modules.car.entity.CarModel;
import com.dyzx.hbb.modules.car.service.CarModelAreaService;
import com.dyzx.hbb.modules.car.service.CarModelService;
import com.dyzx.hbb.modules.car.mapper.CarModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【car_model】的数据库操作Service实现
* @createDate 2025-08-08 14:08:30
*/
@Service
public class CarModelServiceImpl extends ServiceImpl<CarModelMapper, CarModel>
    implements CarModelService{
    @Autowired
    private CarModelAreaService childrenArea;
    @Cacheable(value = "car", key = "'tree::model'+#markId")

    @Override
    public List<Map<String, Object>> findByTreeList(Long markId) {
        List<Map<String, Object>> results=new ArrayList<>();
        MyLambdaQueryWrapper<CarModel> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(CarModel::getFindCount);
        queryWrapper.eq(CarModel::getMarkId,markId);
        //queryWrapper.gt(CarModel::getFindCount,0);

        List<CarModel> list=this.list(queryWrapper);
        for (CarModel carModel : list) {
            Map<String, Object> map=new HashMap<>() ;
            map.put("id",carModel.getId());
            map.put("name",carModel.getModel());
            map.put("logo",carModel.getLogo());
            map.put("mark_id",carModel.getMarkId());
            map.put("trim_level",carModel.getTrimLevel());
            map.put("body_type",carModel.getBodyType());
            map.put("level_name",carModel.getLevelName());
            if(carModel.getFindCount()>0) {
                List<Map<String, Object>> children = childrenArea.findByTreeList(carModel.getId());
                map.put("children", children);
            }else {
                map.put("children", List.of());
            }
            results.add(map);
        }
        return results;

    }
}




